package com.book.util;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Snowflake;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.HashMap;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Configuration
public class MVCConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login-new");
    }
    @Bean
    public LineCaptcha lineCaptcha(){
        return CaptchaUtil.createLineCaptcha(210,90);
    }

    @Bean
    public Snowflake snowflake(){
        return new Snowflake();
    }

    // 用来暂时代替后面我们要学的，redis(缓存)
    @Bean
    public HashMap<String,String> hashMap(){
        return new HashMap<>(16);
    }
    
    
    //将支付宝沙箱对象配置进容器中
    @Bean
    public AlipayTradePagePayRequest alipayTradePagePayRequest(AlipayConfig alipayConfig) {
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(alipayConfig.getReturn_url());
        alipayRequest.setNotifyUrl(alipayConfig.getNotify_url());

        return alipayRequest;
    }

    @Bean
    public AlipayClient alipayClient(AlipayConfig alipayConfig) {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGatewayUrl(), alipayConfig.getApp_id(),
                alipayConfig.getMerchant_private_key(), "json", alipayConfig.getCharset(), alipayConfig.getAlipay_public_key(), alipayConfig.getSign_type());
        return alipayClient;
    }
}
